package com.fsc.handler;

import cn.dev33.satoken.exception.NotLoginException;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 根据不同异常进行处理，得到异常结果
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ResultWrapper<String> handleBusinessException(BusinessException e) {
        return ResultWrapper.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResultWrapper<String> handleException(Exception e) {
        return ResultWrapper.error( e.getMessage());
    }
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResultWrapper<String> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        return ResultWrapper.fail(ReturnEnum.PARAMETER_MISSING,e.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public ResultWrapper<String> handleValidException(BindException e) {
        return ResultWrapper.fail(ReturnEnum.REGEX_FAILED,e.getAllErrors().get(0).getDefaultMessage());
    }
    @ExceptionHandler(NotLoginException.class)
    public ResultWrapper<String> handleNotLoginException(NotLoginException e) {
        return ResultWrapper.fail(ReturnEnum.NOT_LOGIN);
    }
    @ExceptionHandler(FileUploadException.class)
    public ResultWrapper<String> handleFileUploadException(FileUploadException e) {
        return ResultWrapper.fail(ReturnEnum.UPLOAD_FAIL,e.getMessage());
    }

}

